<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title></title>
    <meta name="keywords" content="" />
    <meta name="description" content="">
    <link href="css/bulma.min.css" rel="stylesheet">
    <link href="css/github-markdown.min.css" rel="stylesheet">
    <link rel="stylesheet" href="css/app.css">
    <script src="./js/config.js"></script>
    <script src="./js/jquery.min.js"></script>
</head>

<body>

    <nav id="navbar" class="navbar has-shadow">
        <div class="container">
            <div id="navMenu" class="navbar-menu">
                <div class="navbar-start">
                    <a class="navbar-item" href="index.html">主页</a>
                    <a class="navbar-item" href="about.html">关于</a>
                </div>
                <div class="navbar-end">
                    <div class="navbar-item">
                        <a class="button is-small is-primary" href="category.html">分类</a>
                    </div>
                    <div class="navbar-item">
                        <a class="button is-small is-primary" href="add-article.html">写文章</a>
                    </div>
                    <div class="navbar-item">
                        <a href="user-articlelist.html">张三</a>
                    </div>
                    <div class="navbar-item">
                        <a href="javascript:void(0);">退出</a>
                    </div>
                    <div class="navbar-item">
                        <a class="button is-small is-primary" href="register.html">注册</a>
                    </div>
                    <div class="navbar-item">
                        <a class="button is-small" href="login.html">登录</a>
                    </div>
                </div>
            </div>
        </div>
    </nav>

    <div class="container margin-top">
        <div class="columns">
            <div class="column is-8 is-offset-2">
                <a href="add-category.html" class="button is-small is-primary">新建分类</a>
                <table class="table margin-top is-bordered is-striped is-narrow is-hoverable is-fullwidth">
                    <thead>
                        <tr>
                            <th>分类名</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="list-table">
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>

<script>
    function LoadAritcleInfo() {
        jQuery.ajax({
            url: base_url + "/category/list",
            method: "GET",
            data: {
            },
            // 携带 token 进行是否登录判断
            headers: {
                "Authorization": window.localStorage.getItem(token_key)
            },
            success: function (result) {
                if (result != null && result.code == 200) {
                    // 获取文章分类是一个 List 集合
                    var ListHtml = "";
                    for (let i = 0; i < result.data.length; i++) {
                        // 获取单个对象
                        var category = result.data[i];
                        ListHtml += '<tr>';
                        ListHtml += '<td>' + category.name + '</td>';
                        ListHtml += '<td>';
                        ListHtml += '<a href="javascript:doDel(' + category.cid + ');" class="button is-small is-danger">删除</a>';   // 执行删除标签逻辑
                        ListHtml += '</td>';
                        ListHtml += '</tr>';
                    }
                    jQuery("#list-table").html(ListHtml);
                    return;
                } else {
                    alert("获取文章分类失败！" + result.msg);
                    return;
                }
            }
        });
    }
    LoadAritcleInfo();

    function doDel(cid) {
        if (confirm("确定要删除这个文章标签吗？")) {
            // 验证参数
            if (cid == null || cid == "") {
                alert("删除失败：参数传递错误！");
                return;
            }

            jQuery.ajax({
                url: base_url + "/category/del",
                method: "POST",
                data: {
                    "cid": cid
                },
                // 携带 token 进行是否登录判断
                headers: {
                    "Authorization": window.localStorage.getItem(token_key)
                },
                success: function (result) {
                    if (result != null && result.code == 200) {
                        alert("删除文章分类成功！");
                        location.href = location;
                        return;
                    } else {
                        alert("删除文章分类失败！" + result.msg);
                        return;
                    }
                }
            });
        }
    }

</script>

</html>